package com.superdry.activity;

import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbDialogUtil;
import com.ab.view.wheel.AbWheelUtil;
import com.ab.view.wheel.AbWheelView;
import com.alibaba.fastjson.JSON;
import com.drysurper.R;
import com.superdry.json.Address;
import com.superdry.json.PlaceOrder;
import com.superdry.main.MainActivity;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.ToastHelper;

public class PlaceOrderActivity extends AbActivity implements OnClickListener {
	private TextView name;
	private TextView phone;
	private TextView address;
	private TextView add_address;

	private LinearLayout place_order_goback;
	private View mTimeView = null;
	private TextView timeTextView = null;// 传入放时间
	private LinearLayout order_ll;

	private AbHttpUtil mAbHttpUtil = null;
	private Address address_ = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAbContentView(R.layout.aty_place_order);
		initViews();
	}

	/**
	 * 初始化组件
	 */
	private void initViews() {

		address_ = (Address) getIntent().getSerializableExtra("address");
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setTimeout(10000);

		name = (TextView) findViewById(R.id.name);
		phone = (TextView) findViewById(R.id.phone);
		address = (TextView) findViewById(R.id.address);
		add_address = (TextView) findViewById(R.id.add_address);
		if (address_ != null) {
			name.setText(address_.getPeopleName());
			phone.setText(address_.getAddressPhoneNumber());
			address.setText(address_.getAddressName());
		}

		place_order_goback = (LinearLayout) findViewById(R.id.place_order_goback);
		place_order_goback.setOnClickListener(this);

		timeTextView = (TextView) findViewById(R.id.ordertime_tv);
		order_ll = (LinearLayout) findViewById(R.id.order_ll);
		order_ll.setOnClickListener(this);
		add_address.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.place_order_goback:
			finish();
			break;
		case R.id.order_ll:
			mTimeView = mInflater.inflate(R.layout.wheel_place_order, null);
			initWheelTime(mTimeView, timeTextView);
			AbDialogUtil.showDialog(mTimeView, Gravity.BOTTOM);
			break;
		case R.id.add_address:
			// 添加地址
			Intent intent = new Intent(this, AddressActivity.class);
			startActivityForResult(intent, 0);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {

		if (data != null) {
			address_ = (Address) data.getSerializableExtra("address");
			name.setText(address_.getPeopleName());
			phone.setText(address_.getAddressPhoneNumber());
			address.setText(address_.getAddressName());
		}

	}

	public void initWheelTime(View mTimeView, TextView mText) {
		final AbWheelView mWheelViewMD = (AbWheelView) mTimeView.findViewById(R.id.mWheelViewMD);
		final AbWheelView mWheelViewTime = (AbWheelView) mTimeView.findViewById(R.id.mWheelViewTime);
		Button okBtn = (Button) mTimeView.findViewById(R.id.okBtn);
		Button cancelBtn = (Button) mTimeView.findViewById(R.id.cancelBtn);
		mWheelViewMD.setCenterSelectDrawable(new ColorDrawable(Color.parseColor("#2E38C3EE")));
		mWheelViewTime.setCenterSelectDrawable(new ColorDrawable(Color.parseColor("#2E38C3EE")));
		final String str = AbWheelUtil.initWheelTimePicker(this, mText, mWheelViewMD, mWheelViewTime, okBtn, cancelBtn, 2015, 6, 2, 1, 1, true);
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				AbDialogUtil.removeDialog(PlaceOrderActivity.this);
				// 准备参数
				AbRequestParams params = initParams(str);
				mAbHttpUtil.post(GlobalConst.GET_ADD_ORDER, params, new AbStringHttpResponseListener() {

					@Override
					public void onStart() {

					}

					@Override
					public void onFinish() {

					}

					@Override
					public void onFailure(int statusCode, String content, Throwable error) {
						ToastHelper.show(PlaceOrderActivity.this, "系统错误请稍后再试");
					}

					@Override
					public void onSuccess(int statusCode, String content) {

						List<PlaceOrder> place_orders = JSON.parseArray(content, PlaceOrder.class);
						// 订单提交服务器 成功 跳转到支付页面
						/*Intent intent = new Intent(PlaceOrderActivity.this, PayOrderActivity.class);
						intent.putExtra("order", place_orders.get(0));
						intent.putExtra("address", address.getText().toString());
						startActivity(intent);*/
						Intent intent = new Intent(PlaceOrderActivity.this, MainActivity.class);
						intent.putExtra("cmd", GlobalConst.SHOW_ORDER_FRAGMENT);
						startActivity(intent);
					}
				});
			}
		});
	}

	/**
	 * 准备参数
	 */
	private AbRequestParams initParams(String goHomeTime) {

		AbRequestParams params = new AbRequestParams();
		String[] seasons = { "冬季", "夏季", "春季", "秋季" };
		// 设置 id1 id2 id3 id4
		for (int i = 0; i < 4; i++) {
			int id = SPHelper.getInt(PlaceOrderActivity.this, seasons[i] + "id");
			params.put("id" + (i + 1), id + "");
		}
		// 季节的id 和 对应的数量
		for (int i = 0; i < 4; i++) {
			if (SPHelper.containsInt(PlaceOrderActivity.this, seasons[i])) {
				if ("冬季".endsWith(seasons[i])) {
					params.put("num1", SPHelper.getInt(PlaceOrderActivity.this, seasons[i]) + "");
				} else if ("夏季".endsWith(seasons[i])) {
					params.put("num2", SPHelper.getInt(PlaceOrderActivity.this, seasons[i]) + "");
				} else if ("春季".endsWith(seasons[i])) {
					params.put("num3", SPHelper.getInt(PlaceOrderActivity.this, seasons[i]) + "");
				} else if ("秋季".endsWith(seasons[i])) {
					params.put("num4", SPHelper.getInt(PlaceOrderActivity.this, seasons[i]) + "");
				}
			}
		}
		// 总金额
		int total = SPHelper.getInt(PlaceOrderActivity.this, SPHelper.TOATAL_MONEY);
		params.put("allMoney", total + "");
		// 上门时间
		params.put("goHomeTime", goHomeTime);
		int id = address_.getAddressId();
		// 上门地址id
		params.put("addressId", id + "");
		// 用户id
		params.put("userID", SPHelper.getUserInfo(PlaceOrderActivity.this).getUserID() + "");
		return params;
	}
}
